<?
$db=new SQLiteDatabase("db/mandi");
$result=$db->query("select id, name_en from product_table");
$products = array();
while($row=$result->fetch(SQLITE_ASSOC)){
	$products[] = $row;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Admin</title>


	<link type="text/css" rel="stylesheet" href="ex.css?3.2"/>
    <script type="text/javascript" src="protovis-3.2/protovis-r3.2.js"></script>
    <script type="text/javascript" src="line.js"></script>
    <style type="text/css">
	#fig {
  width: 430px;
  height: 225px;
}
	</style>
</head>

<body> 
<? include 'constants.php';
	include 'navbar.php'; ?>
<div id="center">
 <script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script>
 <script src="http://yui.yahooapis.com/2.9.0/build/event/event-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/connection/connection_core-min.js"></script>
<script src="http://yui.yahooapis.com/2.9.0/build/json/json-min.js"></script>   
<? //include 'navbar.php';
	if($success_msg){ echo '<p>Exit update successful.</p>'; }
	if($error_msg) { echo '<p>'.$error_msg.'</p>'; } ?>
	<select id="product_selection" onchange="graph();" style="position:relative;left:200px;top:50px;">
		<? $default = true;
		foreach($products as $p): ?>
			<option value="<?=$p['id']?>" <?= ($default) ? 'selected="selected"' : '' ; ?>><?=$p['name_en']?></option>
			<? if($default){
					$default = false;
				}?>
		<? endforeach; ?>
	</select>
<div id="fig" style="width:0;height:0;"></div>
<script type="text/javascript+protovis">

function graph() {

	var spans = document.getElementsByTagName('SPAN');
console.log('graph', spans.length);
	if(spans.length > 0){
		for(var s in spans){
			if(s == 0){
				var child = spans[s];
				var parent = child.parentNode;
				console.log(s, child, parent);
				parent.removeChild(child);
			}
		}
	}

	var timecutoff = <?=time()-(24*60*60)?>;
	timecutoff = timecutoff*1000;
	var selectBox = document.getElementById('product_selection');
			var product_id = selectBox[selectBox.selectedIndex].value;

	var datas;
	var url = '<?=BASE_URL?>webservice?action=getprice&id='+product_id;
	xmlHttp = new XMLHttpRequest();
	xmlHttp.onreadystatechange = process;
	xmlHttp.open("GET", url, false);
	xmlHttp.send(null);

	function process(){
		if(xmlHttp.responseText != ''){
			datas = JSON.parse(xmlHttp.responseText);
		}
	}
	var products = datas['data'];
	//console.log('there', products);

	  /* Sizing and scales. */
	var w = 600,
		h = 200,
		x = pv.Scale.linear(0, (datas['x_limit']*1000)).range(0, w),
		y = pv.Scale.linear(datas['low']-1, datas['high']+1).range(0, h);

	/* The root panel. */
	var vis = new pv.Panel()
		.width(w)
		.height(h)
		.bottom(20)
		.left(10)
		.right(10)
		.top(5);

		
	/* X-axis ticks. */
	vis.add(pv.Rule)
		.data(x.ticks(5))
		.visible(function(d) d > 0)
		.left(x)
		.strokeStyle("#eee")
	  .add(pv.Rule)
		.bottom(-5)
		.height(5)
		.strokeStyle("#000")
	  .anchor("bottom").add(pv.Label)
		.text(function(s) { d = new Date(timecutoff+s);
							r = (d.getMonth()+1)+"/"+d.getDate();
							h = d.getHours();
							m = d.getMinutes();
							if((h/10) < 1){ h = "0"+h;}
							if((m/10) < 1){ m = "0"+m;}
							r = r+" "+h+":"+m;
							return r; });
		//.text(x.tickFormat);

	/* Y-axis ticks. */
	vis.add(pv.Rule)
		.data(y.ticks(5))
		.bottom(y)
		.strokeStyle(function(d) d ? "#eee" : "#000")
		.left(15)
	  .anchor("left").add(pv.Label)
		.text(y.tickFormat);

	/* The line. */
	vis.add(pv.Line)
		.data(products)
		.left(function(d) x(d.timediff))
		.bottom(function(d) y(d.rate))
		.lineWidth(3);

	vis.render();
}

graph();


</script>

</div> </body>
</html>
